package com.partsoft.dits.dsm;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.util.Assert;

import com.partsoft.dits.MonitorIntegrateDescriptor;
import com.partsoft.dits.MonitorStartMode;

public class DataSourceMonitorDescriptor implements MonitorIntegrateDescriptor, BeanNameAware {

	/**
	 * 数据源名称
	 */
	private String datasource;

	/**
	 * 启动模式
	 */
	private MonitorStartMode startMode = MonitorStartMode.AUTO;

	/**
	 * 获取间隔（秒）
	 */
	private long monitorInterval = DEFAULT_MONITOR_INTERVAL;

	/**
	 * 获取监听个数据
	 */
	private int retrieveCount = DEFAULT_RETRIEVE_COUNT;

	private boolean removeAfterRetrieved = true;

	private String title;

	private String description;

	private int ordinal = 0;

	public DataSourceMonitorDescriptor() {
	}

	public DataSourceMonitorDescriptor(String datasource, MonitorStartMode startMode, boolean removeAfterretrieve,
			long retrieveInterval, int retrieveCount) {
		super();
		this.datasource = datasource;
		this.startMode = startMode;
		this.removeAfterRetrieved = removeAfterretrieve;
		this.monitorInterval = retrieveInterval;
		this.retrieveCount = retrieveCount;
	}

	public String getDatasourceName() {
		return datasource;
	}

	public void setDatasourceName(String datasourceName) {
		Assert.hasText(datasourceName, "必须设置数据源名称。");
		this.datasource = datasourceName;
	}

	public MonitorStartMode getStartMode() {
		return startMode;
	}

	public void setStartMode(MonitorStartMode startMode) {
		this.startMode = startMode;
	}

	public int getRetrieveCount() {
		return retrieveCount;
	}

	public void setRetrieveCount(int retrieveCount) {
		this.retrieveCount = retrieveCount;
	}

	public boolean isRemoveAfterRetrieved() {
		return removeAfterRetrieved;
	}

	public void setRemoveAfterRetrieved(boolean removeAfterRetrieved) {
		this.removeAfterRetrieved = removeAfterRetrieved;
	}

	@Override
	public String toString() {
		return String.format("Monitor descriptor of DataSource (%s)", getId());
	}

	public String getTitle() {
		return title == null ? getDatasourceName() : title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description == null ? "" : description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public long getMonitorInterval() {
		return monitorInterval;
	}

	public void setMonitorInterval(long monitorInterval) {
		this.monitorInterval = monitorInterval;
	}

	public void setBeanName(String beanName) {
		setDatasourceName(beanName);
	}

	public String getId() {
		return getDatasourceName();
	}

	@Override
	public DataSourceMonitorDescriptor clone() {
		try {
			return (DataSourceMonitorDescriptor) super.clone();
		} catch (CloneNotSupportedException e) {
			return null;
		}
	}

	public int getOrdinal() {
		return ordinal;
	}

	public void setOrdinal(int ordinal) {
		this.ordinal = ordinal;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((getDatasourceName() == null) ? 0 : getDatasourceName().hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		DataSourceMonitorDescriptor other = (DataSourceMonitorDescriptor) obj;
		if (getDatasourceName() == null) {
			if (other.getDatasourceName() != null)
				return false;
		} else if (!getDatasourceName().equals(other.getDatasourceName()))
			return false;
		return true;
	}

}
